package com.aliyun.messagemgt.web.controller;

import com.aliyun.messagemgt.application.service.MsgSourceService;
import com.aliyun.messagemgt.application.service.OperateLogService;
import com.aliyun.messagemgt.application.service.sms.SmsQuotaConfigService;
import com.aliyun.messagemgt.application.service.sms.SmsQuotaService;
import com.aliyun.messagemgt.common.constants.MgtCommonConst;
import com.aliyun.messagemgt.common.enums.OperateTypeEnum;
import com.aliyun.messagemgt.common.enums.ResourceTypeEnum;
import com.aliyun.messagemgt.common.errorcode.CommonErrorCode;
import com.aliyun.messagemgt.common.exceptions.BaseException;
import com.aliyun.messagemgt.dto.MsgSource;
import com.aliyun.messagemgt.dto.OperateLog;
import com.aliyun.messagemgt.dto.SmsQuotaDefaultConfig;
import com.aliyun.messagemgt.dto.vo.DepVo;
import com.aliyun.messagemgt.dto.vo.SmsQuotaRequestVO;
import com.aliyun.messagemgt.dto.vo.SmsQuotaResponseVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 短信配额 controller
 *
 * @author byp
 * @date 2020/7/10
 */
@Api(tags = "短信配额相关接口")
@CrossOrigin
@RestController
@RequestMapping("/quota")
public class SmsQuotaController {

    @Autowired
    private SmsQuotaService smsQuotaService;
    @Autowired
    private SmsQuotaConfigService smsQuotaConfigService;
    @Autowired
    private OperateLogService operateLogService;
    @Autowired
    private MsgSourceService msgSourceService;

    @ApiOperation(value = "查询短信配额列表信息")
    @PostMapping("/list")
    @ResponseBody
    @ApiResponses(
            @ApiResponse(code = 200, message = "success", response= SmsQuotaResponseVO.class)
    )
    public Object smsQuotaList(@ApiParam(value = "短信配额列表查询参数") @RequestBody SmsQuotaRequestVO smsQuotaRequestVO){
        return smsQuotaService.querySmsQuotaList(smsQuotaRequestVO);
    }

    @ApiOperation(value = "保存短信消息源配置信息")
    @PostMapping("/add")
    public Object smsQuotaAdd(@ApiParam(value = "短信配额入参") @RequestBody SmsQuotaRequestVO smsQuotaRequestVO) {
        MsgSource msgSource = msgSourceService.getMsgSourceBySourceId(smsQuotaRequestVO.getMsgSourceId());
        if(msgSource == null){
            throw new BaseException(CommonErrorCode.FAIL_UPDATE_QUOTA);
        }
        String sourceName = msgSource.getSourceName();
        OperateLog operateLog = new OperateLog()
                .setOperateResourceType(ResourceTypeEnum.QUOTA.getCode())
                .setModule(MgtCommonConst.MODULE_SMART_PUSH)
                .setOperateUser(smsQuotaRequestVO.getUserId())
                .setOperateUserName(smsQuotaRequestVO.getUserName())
                .setOperateDept(smsQuotaRequestVO.getDepartment())
                .setOperateDeptName(smsQuotaRequestVO.getDepartmentName())
                .setOperateTime(new Date())
                .setOperateResourceId(Math.toIntExact(msgSource.getMsgSourceId()))
                .setOperateResourceName(sourceName);
        int i = 0;
        try {
            operateLog.setOperateType(OperateTypeEnum.MOD.getCode());
            i = (int)smsQuotaService.smsQuotaAdd(smsQuotaRequestVO);
            if(i <= 0){
                operateLog.setFailMsg(CommonErrorCode.FAIL_UPDATE_QUOTA.errName());
                operateLog.setOperateStatus(MgtCommonConst.OPERATER_FAIL);
            }else{
                operateLog.setFailMsg(CommonErrorCode.SUCCESS_UPDATE_QUOTA.errName());
                operateLog.setOperateStatus(MgtCommonConst.OPERATER_SUCCESS);
            }
        }catch (Exception e){
            operateLog.setFailMsg(CommonErrorCode.FAIL_UPDATE_QUOTA.errName());
            operateLog.setOperateStatus(MgtCommonConst.OPERATER_FAIL);
            if(e instanceof BaseException || e instanceof RuntimeException){
                throw e;
            }else{
                throw new RuntimeException("操作失败！");
            }
        }finally {
            operateLogService.insertOperateLog(operateLog);
        }
        return i;
    }

    @ApiOperation(value = "保存短信默认配额")
    @PostMapping("/config/add")
    public Object smsQuotaConfigAdd(@ApiParam(value = "短信配额入参") @RequestBody SmsQuotaRequestVO smsQuotaRequestVO){
        OperateLog operateLog = new OperateLog()
                .setOperateResourceType(ResourceTypeEnum.QUOTA.getCode())
                .setModule(MgtCommonConst.MODULE_SMART_PUSH)
                .setOperateUser(smsQuotaRequestVO.getUserId())
                .setOperateUserName(smsQuotaRequestVO.getUserName())
                .setOperateDept(smsQuotaRequestVO.getDepartment())
                .setOperateDeptName(smsQuotaRequestVO.getDepartmentName())
                .setOperateTime(new Date())
                .setOperateResourceId(null)
                .setOperateResourceName("默认配额");
        int i = 0;
        try {
            operateLog.setOperateType(OperateTypeEnum.MOD.getCode());
            i = (int)smsQuotaConfigService.smsQuotaConfigAdd(smsQuotaRequestVO);
            if(i <= 0){
                operateLog.setFailMsg(CommonErrorCode.FAIL_DEFAULT_QUOTA.errName());
                operateLog.setOperateStatus(MgtCommonConst.OPERATER_FAIL);
            }else{
                operateLog.setFailMsg(CommonErrorCode.SUCCESS_DEFAULT_QUOTA.errName());
                operateLog.setOperateStatus(MgtCommonConst.OPERATER_SUCCESS);
            }
        }catch (Exception e){
            operateLog.setFailMsg(CommonErrorCode.FAIL_DEFAULT_QUOTA.errName());
            operateLog.setOperateStatus(MgtCommonConst.OPERATER_FAIL);
            if(e instanceof BaseException){
                throw e;
            }else{
                throw new RuntimeException("操作失败！");
            }
        }finally {
            operateLogService.insertOperateLog(operateLog);
        }
        return i;
    }

    @ApiOperation(value = "查询默认短信配额")
    @PostMapping("/config/history")
    @ResponseBody
    @ApiResponses(
            @ApiResponse(code = 200, message = "success", response= SmsQuotaDefaultConfig.class)
    )
    public Object smsQuotaConfigHistory(){
        return smsQuotaConfigService.smsQuotaConfigHistory();
    }

    @ApiOperation(value = "查询短信配额列表时部门筛选列表")
    @PostMapping("/ownerDept")
    @ResponseBody
    @ApiResponses(
            @ApiResponse(code = 200, message = "success", response= DepVo.class)
    )
    public Object smsQuotaDeptList(@ApiParam(value = "短信配额入参") @RequestBody SmsQuotaRequestVO smsQuotaRequestVO){
        return smsQuotaService.smsQuotaDeptList(smsQuotaRequestVO);
    }
}
